<?php

namespace app\manage\controller;

use app\common\validate\system\SystemDepartmentValid;
use app\manage\loader\ManageServices;
use think\Exception;

class SystemDepartment extends BaseApi
{
    /**
     * 部门列表分页
     */
    public function page()
    {
        $params = $this->request->get();
        try {
            $option = $this->formatOption($params);
            if (!empty($params['keyword'])) {
                $where[] = ['title', 'like', "%" . $params['keyword'] . "%"];
            }
            if (isset($params['pid']) && $params['pid'] !== '') {
                $where[] = ['pid', '=', $params['pid']];
            }
            $option['where'] = $where ?? [];
            $result = ManageServices::SystemDepartment()->getPage($option);
            api_result($result);
        } catch (Exception $e) {
            api_result($e);
        }

    }

    /**
     * 部门添加编辑
     */
    public function save()
    {
        $params = $this->request->post();
        try {
            $params['op_user'] = $this->opUser;
            $scene = '.create';
            if (!empty($params['id'])) {
                unset($params['op_user']);
                $scene = '.edit';
            }
            $this->validate($params, SystemDepartmentValid::class . $scene);
            ManageServices::SystemDepartment()->saveDept($params);
            api_result(API_SUCCESS, '保存成功');
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 部门添加编辑
     */
    public function save_sort()
    {
        $params = $this->request->post();
        try {
            $this->validate($params, SystemDepartmentValid::class . '.edit_sort');
            ManageServices::SystemDepartment()->editDept(['id' => $params['id']], ['sort' => $params['sort']]);
            api_result(API_SUCCESS, '保存成功');
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 部门树形列表
     */
    public function tree_list()
    {
        try {
            $option = [];
            if (!empty($this->adminUser['sub_admin_ids'])) {
                $admin_id = $this->adminUser['sub_admin_ids'];
                $where[] = ['id', 'in', function ($query) use ($admin_id) {
                    $query->name('system_manager')->whereIn('id', $admin_id)->field('dept_id');
                }];
                $option['where'][] = $where;
            }
            $result = ManageServices::SystemDepartment()->getList($option, 1);
            api_result($result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 部门详情
     */
    public function info()
    {
        try {
            $id = $this->request->get('id');
            if (empty($id)) {
                api_exception(400, '部门ID不能为空');
            }
            $option['where'] = [['id', '=', $id]];
            $info = ManageServices::SystemDepartment()->getInfo($option);
            api_result($info);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 部门列表
     */
    public function list()
    {
        try {
            $params = $this->request->get();
            if (!empty($this->adminUser['sub_admin_ids'])) {
                $admin_id = $this->adminUser['sub_admin_ids'];
                $where[] = ['id', 'in', function ($query) use ($admin_id) {
                    $query->name('system_manager')->whereIn('id', $admin_id)->field('dept_id');
                }];
            }
            if (isset($params['pid']) && $params['pid'] !== '') {
                $where[] = ['pid', '=', $params['pid']];
            }
            $option['where'] = $where ?? [];
            $result = ManageServices::SystemDepartment()->getList($option, 0);
            api_result($result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 删除部门
     */
    public function del()
    {
        try {
            $params = $this->request->post();
            if (empty($params['id'])) {
                api_exception(400, '部门ID不能为空');
            }
            ManageServices::SystemDepartment()->del($params['id']);
            api_result(API_SUCCESS, '删除成功');
        } catch (Exception $e) {
            api_result($e);
        }
    }
}
